package com.shouer.doc.ui.forgetpassword;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.shouer.doc.R;
import com.shouer.doc.dialog.ShouerLoadingDialogUtil;
import com.shouer.doc.ui.forgetpassword.presenter.ForgetPassPresenterCompl;
import com.shouer.doc.ui.forgetpassword.presenter.IForgetPassPresenter;
import com.shouer.doc.ui.forgetpassword.view.IForgetPassView;
import com.shouer.doc.widget.TimeText;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import butterknife.OnTextChanged;

/**
 * Created by ZXL on 2016/12/8.
 */

public class ForgetPasswordActivity extends AppCompatActivity implements IForgetPassView{

    @BindView(R.id.phone_input)
    protected EditText phoneInput;
    @BindView(R.id.check_num_input)
    protected EditText checkNumInput;
    @BindView(R.id.password_input)
    protected EditText passwordInput;
    @BindView(R.id.password_input_again)
    protected EditText passwordInputAgain;
    @BindView(R.id.check_num_btn)
    protected View checkNumBtn;
    @BindView(R.id.get_sms_btn)
    protected TimeText getTimeBtn;
    @BindView(R.id.register_btn)
    protected View registerBtn;

    @BindView(R.id.check_num_layout)
    protected View checkNumLayout;
    @BindView(R.id.password_layout)
    protected View registerLayout;

    @BindView(R.id.del_tel)
    protected View delTel;
    @BindView(R.id.del_btn1)
    protected View delBtn1;
    @BindView(R.id.del_btn2)
    protected View delBtn2;
    @BindView(R.id.del_sms)
    protected View delSMS;

    private IForgetPassPresenter presenter;
    private String phone;
    private String password;

    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.activity_forget_password_layout);
        ButterKnife.bind(this);
        presenter=new ForgetPassPresenterCompl(this);
    }

    @OnClick(value = {R.id.back_btn, R.id.check_num_btn, R.id.register_btn, R.id.get_sms_btn,R.id.del_tel,R.id.del_sms,R.id.del_btn1,R.id.del_btn2})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.check_num_btn:
                checkNumBtn.setClickable(false);
                phone = phoneInput.getText().toString();
                if (phone.length()<11) {
                    Toast.makeText(this, "输入的手机号不正确", Toast.LENGTH_SHORT).show();
                    checkNumBtn.setClickable(true);
                    return;
                }
                String smsCode = checkNumInput.getText().toString();
                if (TextUtils.isEmpty(smsCode)) {
                    Toast.makeText(this, "校验码不能空", Toast.LENGTH_SHORT).show();
                    checkNumBtn.setClickable(true);
                    return;
                }
                ShouerLoadingDialogUtil.getInstance().show(this);
                presenter.checkSMS(phone,smsCode);
                break;
            case R.id.register_btn:
                registerBtn.setClickable(false);
                password = passwordInput.getText().toString().trim();
                if (password.length() < 6) {
                    Toast.makeText(this, "密码不能少于6位", Toast.LENGTH_SHORT).show();
                    registerBtn.setClickable(true);
                    return;
                }
                String password2 = passwordInputAgain.getText().toString().trim();
                if (!password2.equals(password)) {
                    Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    registerBtn.setClickable(true);
                    return;
                }
                ShouerLoadingDialogUtil.getInstance().show(this);
                presenter.forget(phone,password);
                break;
            case R.id.get_sms_btn:
                phone = phoneInput.getText().toString();
                if (phone.length()<11) {
                    Toast.makeText(this, "输入的手机号不正确", Toast.LENGTH_SHORT).show();
                    return;
                }
                ((TimeText)view).startCountDownTimer();
                presenter.getSMS(phone);
                break;
            case R.id.del_tel:
                phoneInput.setText("");
                break;
            case R.id.del_sms:
                checkNumInput.setText("");
                break;
            case R.id.del_btn1:
                passwordInput.setText("");
                break;
            case R.id.del_btn2:
                passwordInputAgain.setText("");
                break;
        }
    }

    @OnFocusChange(value = {R.id.phone_input,R.id.check_num_input,R.id.password_input,R.id.password_input_again})
    public void onFocusChange(View view,boolean hasFocus)
    {
        switch (view.getId())
        {
            case R.id.phone_input:
                if (hasFocus&&phoneInput.getText().length()>0)
                {
                    delTel.setVisibility(View.VISIBLE);
                }else
                {
                    delTel.setVisibility(View.GONE);
                }
                break;
            case R.id.check_num_input:
                if (hasFocus&&checkNumInput.getText().length()>0)
                {
                    delSMS.setVisibility(View.VISIBLE);
                }else
                {
                    delSMS.setVisibility(View.GONE);
                }
                break;
            case R.id.password_input:
                if (hasFocus&&passwordInput.getText().length()>0)
                {
                    delBtn1.setVisibility(View.VISIBLE);
                }else
                {
                    delBtn1.setVisibility(View.GONE);
                }
                break;
            case R.id.password_input_again:
                if (hasFocus&&passwordInputAgain.getText().length()>0)
                {
                    delBtn2.setVisibility(View.VISIBLE);
                }else
                {
                    delBtn2.setVisibility(View.GONE);
                }
                break;

        }
    }

    @OnTextChanged(value = {R.id.phone_input},callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void afterTextChanged0(Editable s)
    {
        if (phoneInput.hasFocus())
        {
            if (s.length()>0)
            {
                delTel.setVisibility(View.VISIBLE);
            }else
            {
                delTel.setVisibility(View.GONE);
            }
        }
    }

    @OnTextChanged(value = {R.id.password_input},callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void afterTextChanged1(Editable s)
    {
        if (passwordInput.hasFocus())
        {
            if (s.length()>0)
            {
                delBtn1.setVisibility(View.VISIBLE);
            }else
            {
                delBtn1.setVisibility(View.GONE);
            }
        }
    }

    @OnTextChanged(value = {R.id.password_input_again},callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void afterTextChanged2(Editable s)
    {
        if (passwordInputAgain.hasFocus())
        {
            if (s.length()>0)
            {
                delBtn2.setVisibility(View.VISIBLE);
            }else
            {
                delBtn1.setVisibility(View.GONE);
            }
        }
    }

    @OnTextChanged(value = {R.id.check_num_input},callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void afterTextChanged3(Editable s)
    {
        if (checkNumInput.hasFocus())
        {
            if (s.length()>0)
            {
                delSMS.setVisibility(View.VISIBLE);
            }else
            {
                delSMS.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        presenter.onDestroy();
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void getSmsSuccess() {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        Toast.makeText(this,"获取短信验证码成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getSmsFailed(String failInfo) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        Toast.makeText(this,failInfo,Toast.LENGTH_SHORT).show();
        getTimeBtn.cancelCountDownTimer();
    }

    @Override
    public void checkNumSuccess() {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        checkNumLayout.setVisibility(View.GONE);
        registerLayout.setVisibility(View.VISIBLE);
    }

    @Override
    public void checkNumFailed(String info) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        checkNumBtn.setClickable(true);
        Toast.makeText(this,info,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void forgetSuccess() {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        Toast.makeText(this,"修改密码成功",Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void forgetFailed(String info) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        Toast.makeText(this,info,Toast.LENGTH_SHORT).show();
        registerBtn.setClickable(true);
    }

}
